home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 4 / BBS in a Box - Macintosh - Volume IV (January 1992) (BBS in a Box).iso / Files / Prog / M / MYWINDOW.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-04-26  |  10.9 KB  |  356 lines  |  [TEXT/MACA]

  1. PROGRAM my_window;
  2.  
  3.                { QDSample -- Macintosh adaptation of Lisa QuickDraw example. }
  4.                                 { by Paul Zemlin, Macintosh Technical Support}
  5.  
  6.                                       { Modified for Turbo Pascal, June 1986 }
  7.  
  8. {$R mywindow.rsrc}
  9. {$U-}
  10.  
  11.    USES Memtypes,QuickDraw,OSIntf,ToolIntf;           {toolbox programs used}
  12.  
  13.    TYPE
  14.       IconData = ARRAY[0..95] OF INTEGER;             { a 'type' created  
  15.                                                  especially for this program}
  16.  
  17.    CONST
  18.       lastMenu = 2;                                        { number of menus }
  19.       appleMenu = 1;                       { menu ID for desk accessory menu }
  20.       fileMenu = 256;                                { menu ID for File menu }
  21.       
  22.    VAR
  23.       myMenus                           : ARRAY [1..lastMenu] OF MenuHandle;
  24.       dragRect ,prect, growrect         : Rect;
  25.       doneFlag,temp                     : BOOLEAN;
  26.       myEvent                           : EventRecord;
  27.       code, refNum, MyControl,t         : INTEGER;
  28.       theMenu, theItem, whichIcon       : INTEGER;
  29.       scale                             : INTEGER;
  30.       wRecord                           : WindowRecord;
  31.       theWindow, whichWindow            : WindowPtr;
  32.       icons                             : ARRAY[0..5] OF IconData;
  33.       hScroll, vScroll, whichControl    : ControlHandle;
  34.       theOrigin                         : Point;
  35.       theUpdateRgn                      : RgnHandle;
  36.  
  37.  
  38.   
  39.  
  40.   
  41.    PROCEDURE MoveScrollBars;  {this procedure will be looked for when invoked
  42.                                 by subsequent procedure involving the window}
  43.  
  44.       BEGIN
  45.          WITH theWindow^.portRect DO
  46.             BEGIN
  47.             HideControl(vScroll);
  48.             MoveControl(vScroll,right-15,top-1);
  49.             SizeControl(vScroll,16,bottom-top-13);
  50.             ShowControl(vScroll);
  51.             HideControl(hScroll);
  52.             MoveControl(hScroll,left-1,bottom-15);
  53.             SizeControl(hScroll,right-left-13,16);
  54.             ShowControl(hScroll)
  55.             END
  56.       END;
  57.  
  58.    PROCEDURE ResizePRect;          { pRect is the window's content region,
  59.                                      minus the scroll bars }
  60.  
  61.       BEGIN
  62.          pRect := thePort^.portRect;
  63.          pRect.right := pRect.right-15;
  64.          pRect.bottom := pRect.bottom-15
  65.       END;
  66.  
  67.    PROCEDURE GrowWnd (whichWindow: WindowPtr);
  68.  
  69.                     { Handles growing and sizing the window and manipulating }
  70.                                         { the update region. }
  71.  
  72.       VAR
  73.          longResult: LongInt;
  74.          height,width: INTEGER;
  75.          tRect: Rect;
  76.  
  77.       BEGIN
  78.          longResult := GrowWindow(whichWindow,myEvent.where,growRect);
  79.          IF longResult=0 THEN EXIT;
  80.          height := HiWord(longResult); width := LoWord(longResult);
  81.  
  82.          { Add the old "scroll bar area" to the update region so it will }
  83.          { be redrawn (for when the window is enlarged). }
  84.          tRect := whichWindow^.portRect;
  85.          tRect.left := tRect.right - 15;
  86.          InvalRect(tRect);
  87.          tRect := whichWindow^.portRect;
  88.          tRect.top := tRect.bottom - 15;
  89.          InvalRect(tRect);
  90.  
  91.  
  92.          { Now draw the newly sized window. }
  93.          
  94.          
  95.          SizeWindow(whichWindow,width,height,TRUE);
  96.          MoveScrollBars;
  97.          ResizePRect;
  98.  
  99.          { Add the new "scroll bar area" to the update region so it will }
  100.          { be redrawn (for when the window is made smaller). }
  101.          
  102.          tRect := whichWindow^.portRect; tRect.left := tRect.right-15;
  103.          InvalRect(tRect);
  104.          tRect := whichWindow^.portRect; tRect.top := tRect.bottom-15;
  105.          InvalRect(tRect);
  106.       END; { of GrowWnd }
  107.  
  108.    PROCEDURE DrawWindow(whichWindow: WindowPtr);
  109.    { Draws the content region of theWindow }
  110.  
  111.       VAR
  112.          tRect    : Rect;
  113.  
  114.       BEGIN
  115.  
  116.          ClipRect (theWindow^.portRect);
  117.          DrawGrowIcon(theWindow);
  118.          IF theWindow = FrontWindow THEN DrawControls(theWindow);
  119.  
  120.          { Now set up a clip area which excludes the scroll bars }
  121.  
  122.  
  123.          tRect := theWindow^.portRect;
  124.          tRect.bottom := tRect.bottom - 15;
  125.          tRect.right := tRect.right - 15;
  126.  
  127.          {Now compensate for any scrolling which has been done }
  128.  
  129.          OffsetRect (tRect, theOrigin.h, theOrigin.v);
  130.          ClipRect (tRect);
  131.  
  132.          { Change the origin to compensate for any scrolling which has
  133.           been done }
  134.  
  135.          SetOrigin (theOrigin.h, theOrigin.v);
  136.         
  137.          SetOrigin (0, 0);
  138.          ClipRect (theWindow^.portRect);            { Reset the clip area }
  139.        END; { of DrawWindow }
  140.  
  141.    PROCEDURE ScrollBits;
  142.  
  143.       VAR
  144.          oldOrigin   : point;
  145.          dh,dv       : INTEGER;
  146.          tRect       : Rect;
  147.  
  148.       BEGIN
  149.             oldOrigin := theOrigin;
  150.             theOrigin.h := 4 * GetCtlValue(hScroll);
  151.             theOrigin.v := 4 * GetCtlValue(vScroll);
  152.             dh := oldOrigin.h - theOrigin.h;
  153.             dv := oldOrigin.v - theOrigin.v;
  154.             theUpdateRgn := NewRgn;
  155.             ScrollRect (pRect, dh, dv, theUpdateRgn);
  156.  
  157.             { Have scrolled in junk...need to redraw }
  158.  
  159.             SetOrigin (theOrigin.h, theOrigin.v);
  160.             OffsetRect (theUpdateRgn^^.rgnBBox, theOrigin.h, theOrigin.v);
  161.             ClipRect (theUpdateRgn^^.rgnBBox);
  162.            
  163.             DisposeRgn (theUpdateRgn);
  164.             SetOrigin (0, 0);
  165.             ClipRect (theWindow^.portRect);
  166.       END;
  167.  
  168.    PROCEDURE ScrollUp(whichControl: ControlHandle; theCode: INTEGER);
  169.  
  170.       BEGIN
  171.          IF theCode=inUpButton THEN
  172.             BEGIN
  173.             SetCtlValue(whichControl,GetCtlValue(whichControl)-1);
  174.             ScrollBits
  175.             END
  176.       END;
  177.  
  178.    PROCEDURE ScrollDown(whichControl: ControlHandle; theCode: INTEGER);
  179.  
  180.       BEGIN
  181.          IF theCode=inDownButton THEN
  182.             BEGIN
  183.             SetCtlValue(whichControl,GetCtlValue(whichControl)+1);
  184.             ScrollBits
  185.             END
  186.       END;
  187.  
  188.    PROCEDURE PageScroll(code,amount: INTEGER);
  189.  
  190.       VAR
  191.          myPt: point;
  192.  
  193.       BEGIN
  194.          REPEAT
  195.             GetMouse(myPt);
  196.             IF TestControl(whichControl,myPt)=code THEN
  197.                BEGIN
  198.                SetCtlValue(whichControl,GetCtlValue(whichControl)+amount);
  199.                ScrollBits
  200.                END
  201.          UNTIL NOT StillDown;
  202.       END;
  203.  
  204.    PROCEDURE SetUpMenus;
  205.    { Once-only initialization for menus }
  206.  
  207.       VAR
  208.          i: INTEGER;
  209.  
  210.       BEGIN
  211.          InitMenus; { initialize Menu Manager }
  212.          myMenus[1] := GetMenu(appleMenu);
  213.          AddResMenu(myMenus[1],'DRVR'); { desk accessories }
  214.          myMenus[2] := GetMenu(fileMenu);
  215.          FOR i := 1 TO lastMenu DO InsertMenu(myMenus[i],0);
  216.          DrawMenuBar;
  217.       END; { of SetUpMenus }
  218.  
  219.  
  220.    PROCEDURE DoCommand(mResult: LongInt);
  221.  
  222.       VAR
  223.          name: STR255;
  224.  
  225.       BEGIN
  226.          theMenu := HiWord(mResult); theItem := LoWord(mResult);
  227.          CASE theMenu OF
  228.  
  229.             appleMenu:
  230.                BEGIN
  231.                GetItem(myMenus[1],theItem,name);
  232.                refNum := OpenDeskAcc(name);
  233.                END;
  234.  
  235.             fileMenu: doneFlag := TRUE; { Quit }
  236.  
  237.  
  238.          END; { of menu case }
  239.  
  240.          HiliteMenu(0);
  241.  
  242.       END; { of DoCommand }
  243.  
  244.    BEGIN { main program }
  245.       InitGraf(@thePort);
  246.       InitFonts;
  247.       FlushEvents(everyEvent,0);
  248.       InitWindows;
  249.       SetUpMenus;
  250.       InitDialogs(NIL);
  251.       SetCursor(arrow);
  252.       SetRect(dragRect,4,24,508,338);
  253.       SetRect(growRect,100,60,512,302);
  254.       doneFlag := FALSE;
  255.       InitCursor;
  256.       
  257.  
  258.       theWindow := GetNewWindow(256,@wRecord,POINTER(-1));
  259.       SetPort(theWindow);
  260.       theWindow^.txFont := 2;
  261.  
  262.       ResizePRect;
  263.  
  264.       vScroll := GetNewControl(256,theWindow);
  265.       hScroll := GetNewControl(257,theWindow);
  266.       theOrigin.h := 0; theOrigin.v := 0;
  267.  
  268.  
  269.       REPEAT
  270.          SystemTask;
  271.          temp := GetNextEvent(everyEvent,myEvent);
  272.          CASE myEvent.what OF
  273.  
  274.             mouseDown:
  275.                BEGIN
  276.                code := FindWindow(myEvent.where,whichWindow);
  277.                CASE code OF
  278.  
  279.                   inMenuBar: DoCommand(MenuSelect(myEvent.where));
  280.  
  281.                   inSysWindow: SystemClick(myEvent,whichWindow);
  282.  
  283.                   inDrag: DragWindow(whichWindow,myEvent.where,dragRect);
  284.  
  285.                   inGoAway:
  286.                      IF TrackGoAway(whichWindow,myEvent.where) THEN
  287.                         doneFlag := TRUE;
  288.  
  289.                   inGrow:
  290.                      IF whichWindow=FrontWindow THEN
  291.                         GrowWnd(whichWindow)
  292.                      ELSE
  293.                         SelectWindow(whichWindow);
  294.  
  295.                   inContent:
  296.                      BEGIN
  297.                      IF whichWindow<>FrontWindow THEN
  298.                         SelectWindow(whichWindow)
  299.                      ELSE
  300.                         BEGIN {front}
  301.                         GlobalToLocal(myEvent.where);
  302.                         IF  NOT PtInRect(myEvent.where,pRect) THEN
  303.                            BEGIN {controls}
  304.                            MyControl := FindControl(myEvent.where,whichWindow,
  305.                                                     whichControl);
  306.                            CASE MyControl OF
  307.                               inUpButton:
  308.                                  t := TrackControl(whichControl,myEvent.where,
  309.                                                    @ScrollUp);
  310.                               inDownButton:
  311.                                  t := TrackControl(whichControl,myEvent.where,
  312.                                                    @ScrollDown);
  313.                               inPageUP: PageScroll(MyControl,-10);
  314.                               inPageDown: PageScroll(MyControl,10);
  315.                               inThumb:
  316.                                  BEGIN
  317.                                  t := TrackControl(whichControl,myEvent.where,
  318.                                       NIL);
  319.                                  ScrollBits
  320.                                  END
  321.                            END {Case MyControl}
  322.                            END {controls}
  323.                         END {front}
  324.                      END {in Content}
  325.                END; { of code case }
  326.                END; { of mouseDown }
  327.  
  328.             activateEvt:
  329.                BEGIN
  330.                SetPort (theWindow);
  331.                DrawGrowIcon(theWindow);
  332.                IF ODD(myEvent.modifiers) THEN { window is becoming active }
  333.                   BEGIN
  334.                   ShowControl(vScroll);
  335.                   ShowControl(hScroll);
  336.                   END
  337.                ELSE
  338.                   BEGIN
  339.                   HideControl(vScroll);
  340.                   HideControl(hScroll)
  341.                   END
  342.                END; { of activateEvt }
  343.  
  344.             updateEvt:
  345.                BEGIN
  346.                BeginUpdate(theWindow);
  347.                EraseRect (theWindow^.portRect);
  348.                DrawWindow(theWindow);
  349.                EndUpdate(theWindow);
  350.                END { of updateEvt }
  351.  
  352.          END { of event case }
  353.  
  354.       UNTIL doneFlag
  355.    END.